package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Nation;
import com.ruoyi.system.service.INationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 民族Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"民族"})
@RestController
@RequestMapping("/dict/nation")
public class NationController extends BaseController {
    @Autowired
    private INationService nationService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询民族列表
     */
    @ApiOperation(value = "民族列表", notes = "查询民族列表详情", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Nation nation) {
        startPage();
        List<Nation> list = nationService.selectNationList(nation);
        return getDataTable(list);
    }

    /**
     * 导出民族列表
     */
    @ApiOperation(value = "民族列表导出", notes = "导出民族列表", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:export')")
    @Log(title = "民族", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Nation nation) {
        List<Nation> list = nationService.selectNationList(nation);
        ExcelUtil<Nation> util = new ExcelUtil<Nation>(Nation.class);
        util.exportExcel(response, list, "民族数据");
    }

    /**
     * 获取民族详细信息
     */
    @ApiOperation(value = "获取民族详细信息", notes = "获取民族详细信息详情", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:query')")
    @GetMapping(value = "/{nationId}")
    public AjaxResult getInfo(@PathVariable("nationId") Long nationId) {

        return success(nationService.selectNationByNationId(nationId));
    }

    /**
     * 新增民族
     */
    @ApiOperation(value = "新增民族", notes = "新增民族详情", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:add')")
    @Log(title = "民族", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Nation nation) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        nationService.completeData(nation, userId, 1);
        return toAjax(nationService.insertNation(nation));
    }

    /**
     * 修改民族
     */
    @ApiOperation(value = "修改民族", notes = "修改民族详情", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:edit')")
    @Log(title = "民族", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody Nation nation) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        nationService.completeData(nation, userId, 2);
        return toAjax(nationService.updateNation(nation));
    }

    /**
     * 删除民族
     */
    @ApiOperation(value = "删除民族", notes = "删除民族详情", tags = {"民族管理"})
    //@PreAuthorize("@ss.hasPermi('dict:nation:remove')")
    @Log(title = "民族", businessType = BusinessType.DELETE)
    @DeleteMapping("/{nationIds}")
    public AjaxResult remove(@PathVariable Long[] nationIds) {
        return toAjax(nationService.deleteNationByNationIds(nationIds));
    }
}
